package com.kzj.mls.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.ab.util.AbViewUtil;
import com.kzj.mls.R;
import com.kzj.mls.activity.WebActivity;
import com.kzj.mls.beans.AdBean;
import com.kzj.mls.global.App;
import com.kzj.mls.util.ImageUtil;

import java.util.List;

public class ViewsPagerAdapter extends PagerAdapter {

	private List<AdBean> productList;
	private Context context;
	public ViewsPagerAdapter(List<AdBean> productList,Context context) {
		this.productList = productList;
		this.context=context;
	}

	@Override
	public int getCount() {
		return productList.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public Object instantiateItem(View arg0, final int position) {
		/*SimpleDraweeView draweeView = new SimpleDraweeView(context);
		Uri uri = Uri.parse(productList.get(position).getImageUrl());
		draweeView.setImageURI(uri);

		return draweeView;*/

		ImageView imageView=new ImageView(context);
		ViewGroup.LayoutParams layoutParams =new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		layoutParams.height= AbViewUtil.scaleValue(context,310);
		layoutParams.width=App.getInstance().screenWidth;
		imageView.setLayoutParams(layoutParams);
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		imageView.setBackgroundColor(context.getResources().getColor(R.color.blue_btn_bg_pressed_color));
		App.imageLoader.displayImage(productList.get(position).getAd_img(), imageView, ImageUtil.OptionsNormal());
		imageView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (productList.get(position).getAd_link()!=null&&!productList.get(position).equals("")){
					Intent intent = new Intent(context, WebActivity.class);
					intent.putExtra(WebActivity.WebUrl,productList.get(position).getAd_link());
					context.startActivity(intent);
				}
			}
		});
		//App.displayImageHttpOrFile(productList.get(position).getImageUrl(),imageView, ImageUtil.OptionsNormal());
		((ViewPager) arg0).addView(imageView);
		return imageView;
	}


	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}
}